﻿using System;
using MarriageFertility.Texts;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.CharacterDevelopment;
using TaleWorlds.Core;
using TaleWorlds.SaveSystem;

namespace MarriageFertility.DataType
{
    public class NtrRelation
    {
        [SaveableField(1)] public Hero Lord;
        [SaveableField(2)] public Hero Wife;
        [SaveableField(3)] public CampaignTime NtrTime;
        [SaveableField(4)] public float WifeMood;
        [SaveableField(5)] public float WifeLoyalty;
        [SaveableField(6)] public bool LordLikesWife;

        public NtrRelation(Hero lord, Hero wife, CampaignTime time)
        {
            Lord = lord;
            Wife = wife;
            NtrTime = time;
            WifeMood = 50f;
            WifeLoyalty = 75f;
            LordLikesWife = CalculateLordPreference(lord, wife);
        }

        /// <summary>
        /// Calculates whether the lord likes the wife based on multiple factors.
        /// </summary>
        private bool CalculateLordPreference(Hero lord, Hero wife)
        {
            float preferenceScore = 0f;

            // Factor 1: Wife's Charm skill (scaled contribution)
            int charm = wife.GetSkillValue(DefaultSkills.Charm);
            preferenceScore += charm / 5f; // Each 5 points of Charm adds 1 to score

            // Factor 2: Trait compatibility
            preferenceScore += CalculateTraitCompatibility(lord, wife);

            // Factor 3: Relationship with player (affects lord's willingness)
            int playerRelation = lord.GetRelation(Hero.MainHero);
            preferenceScore += playerRelation / 2f; // Positive relations boost, negative reduce

            // Factor 4: Age compatibility (lords prefer wives within a reasonable age range)
            float ageDifference = Math.Abs(lord.Age - wife.Age);
            preferenceScore += ageDifference < 15f ? 20f : -10f;

            // Factor 5: Cultural alignment
            if (lord.Culture == wife.Culture)
                preferenceScore += 15f;

            // Factor 6: Random factor for variability
            preferenceScore += MBRandom.RandomFloat * 10f;

            // Threshold for liking (adjustable)
            return preferenceScore > 50f;
        }

        /// <summary>
        /// Calculates compatibility based on personality traits from DefaultTraits.
        /// </summary>
        private float CalculateTraitCompatibility(Hero lord, Hero wife)
        {
            float compatibility = 0f;

            // Iterate over personality traits (Mercy, Valor, Honor, Generosity, Calculating)
            foreach (var trait in DefaultTraits.Personality)
            {
                int lordTraitLevel = lord.GetTraitLevel(trait);
                int wifeTraitLevel = wife.GetTraitLevel(trait);

                // Positive compatibility for matching positive or negative traits
                if (lordTraitLevel > 0 && wifeTraitLevel > 0 || lordTraitLevel < 0 && wifeTraitLevel < 0)
                    compatibility += 10f * Math.Min(Math.Abs(lordTraitLevel), Math.Abs(wifeTraitLevel));
                // Negative compatibility for opposing traits
                else if (lordTraitLevel > 0 && wifeTraitLevel < 0 || lordTraitLevel < 0 && wifeTraitLevel > 0)
                    compatibility -= 5f * Math.Min(Math.Abs(lordTraitLevel), Math.Abs(wifeTraitLevel));
            }

            return compatibility;
        }
        
        public void UpdateLordPreference()
        {
            bool newPreference = CalculateLordPreference(Lord, Wife);
            if (newPreference != LordLikesWife)
            {
                LordLikesWife = newPreference;
                var message = newPreference ? NtrText.ConsequencesLordLikesWife : NtrText.ConsequencesLordDislikesWife;
                message.SetTextVariable("lord", Lord.Name);
                message.SetTextVariable("wife", Wife.Name);
                MBInformationManager.AddQuickInformation(message);
            }
        }
    }
}